package todolist.activities;

import java.util.ArrayList;
import java.util.List;

import todolist.helpers.taskDataSource;
import todolist.helpers.userDataSource;
import todolist.structures.Task;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 
 * Shows the List Screen screen
 * 
 *
 */
public class ListScreenActivity extends ListActivity{
    
	userDataSource userDatasource;
	Button logoutButton;
	taskDataSource datasource;
	List<Task> tasks;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        buildList();
        
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String item = (String) getListAdapter().getItem(position);
		
		if (item.equals("	Add Task"))
		{
			Toast.makeText(this, "Adding item!", Toast.LENGTH_LONG).show();

			// Tell the context that this is a new task, not an edit
			TodoList context = ((TodoList) getApplicationContext());
			context.setNewTask(true);

			Intent i = new Intent(getApplicationContext(), EditActivity.class);
			startActivity(i);
		}
		
		else if (item.equals("	Filter"))
		{
			TodoList context = ((TodoList) getApplicationContext());
			
			Intent i = new Intent(getApplicationContext(), FilterActivity.class);
			startActivity(i);
		}
		
		else
		{
			// Determine the ID and description of the selected task
			int token = item.indexOf(":");
			int ID = Integer.parseInt(item.substring(0, token));
			//String descrip = item.substring(token+2);
			
			// Set the selected task as current
			TodoList context = ((TodoList) getApplicationContext());
			context.setCurrentTaskId(ID);
			context.setNewTask(false);
			
			// Open the edit screen
			Intent i = new Intent(getApplicationContext(), EditActivity.class);
			startActivity(i);
		}
	}
	
	public void buildList(){
		
		// Create an arrayList to hold the tasks
        ArrayList<String> arr = new ArrayList<String>();
        TodoList context = ((TodoList) getApplicationContext());
		int userID = context.getCurrentUserId();
		
		// Open the database for reading
		datasource = new taskDataSource(this);
		datasource.open();
		
		// Creates a list to hold all tasks
		tasks = datasource.getAllTasks();
		// Close the database
		datasource.close();
		
		
		if (context.getTypesToShow().equalsIgnoreCase("All") && context.isShowComplete() )
		{
			// Adds all tasks for the correct user to the list
			for (int i = 0; i<tasks.size(); i++){
				if(tasks.get(i).getUserId()==userID){
					arr.add(tasks.get(i).getId() + ": " + tasks.get(i).getShortDescription());	
				}
			}
			
		}

		else if (context.isFilterByDate() && context.getTypesToShow().equalsIgnoreCase("All")){
			//Filter by date
			for (int i = 0; i<tasks.size(); i++){
				if(tasks.get(i).getUserId()==userID && (dateOrder(tasks.get(i).getDate()) >= dateOrder(context.getFilterDate()))) {
					arr.add(tasks.get(i).getId() + ": " + tasks.get(i).getShortDescription());	
				}
			}
		}
		
		else if (context.getTypesToShow().equalsIgnoreCase("All"))
		{
			// Adds all incomplete tasks of the correct type to the list
			for (int i = 0; i<tasks.size(); i++){
				if(tasks.get(i).getUserId()==userID && tasks.get(i).isComplete()==0)
				{
					arr.add(tasks.get(i).getId() + ": " + tasks.get(i).getShortDescription());
				}
			}
		}
		
		else if (context.isFilterByDate()){
			//Filter by date
			for (int i = 0; i<tasks.size(); i++){
				if(tasks.get(i).getUserId()==userID && (dateOrder(tasks.get(i).getDate()) >= dateOrder(context.getFilterDate()) &&
						tasks.get(i).getTaskType().equalsIgnoreCase(context.getTypesToShow()))) {
					arr.add(tasks.get(i).getId() + ": " + tasks.get(i).getShortDescription());	
				}
			}
		}
		
		else if (context.isShowComplete())
		{
			// Adds all tasks of the correct type to the list
			for (int i = 0; i<tasks.size(); i++){
				if(tasks.get(i).getUserId()==userID
						&& tasks.get(i).getTaskType().equalsIgnoreCase(context.getTypesToShow())){
					arr.add(tasks.get(i).getId() + ": " + tasks.get(i).getShortDescription());	
				}
			}
		}
		else
		{
			// Adds all incomplete tasks of the correct type to the list
			for (int i = 0; i<tasks.size(); i++){
				if(tasks.get(i).getUserId()==userID && tasks.get(i).isComplete()==0
						&& tasks.get(i).getTaskType().equalsIgnoreCase(context.getTypesToShow())){
					arr.add(tasks.get(i).getId() + ": " + tasks.get(i).getShortDescription());	
				}
			}
		}
		
        
        // Add the Add Task option
        arr.add("	Add Task");
        arr.add("	Filter");
        
        // Adds all tasks in the arrayList to array
        String[] arr2 = new String[arr.size()];
        for (int i = 0; i<arr.size(); i++)
        {
        	arr2[i] = arr.get(i);
        }
        
        // Displays the array
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, arr2);
		setListAdapter(adapter);
		
		
	}

	public int dateOrder(String date){
		String[] test = date.split("-");
		for(int i=0; i<test.length; i++) {
			test[i]=test[i].trim();
		}
		int ret = (Integer.valueOf(test[0])*40) + Integer.valueOf(test[1]) + (Integer.valueOf(test[2])*400);
		return ret;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		// Refresh the list displayed
		buildList();
	}
}